package com.artron.mediaartron.ui.fragment.dialog;

import android.app.Dialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.widget.TextView;

import com.artron.baselib.utils.UIUtils;
import com.umeng.analytics.MobclickAgent;

import java.lang.reflect.Field;

import rx.functions.Action0;

import static android.R.id.message;

/**
 * Class description here
 *
 * @author doing
 * @version 1.0.0
 * @since 2017-10-31.
 */

public class NormalDialogFragment extends DialogFragment {

    private String mTitle;
    private String mPositiveText;
    private String mNegativeText;
    private Action0 mPositiveAction;
    private Action0 mNegativeAction;

    private static int position = 1;
    private String mMessage;

    public String getOurTag() {
        return "NormalDialog" + position++;
    }

    public static NormalDialogFragment newInstance(String title, String positiveText,
                                                   String negativeText) {
        NormalDialogFragment fragment = new NormalDialogFragment();
        fragment.init(title, "", positiveText, negativeText);
        return fragment;
    }

    public static NormalDialogFragment newInstance(String title, String message, String positiveText,
                                                   String negativeText) {
        NormalDialogFragment fragment = new NormalDialogFragment();
        fragment.init(title, message, positiveText, negativeText);
        return fragment;
    }

    public NormalDialogFragment(){

    }



    private void init(String title, String message, String positiveText, String negativeText) {
        this.mTitle = title;
        this.mPositiveText = positiveText;
        this.mNegativeText = negativeText;
        this.mMessage = message;
    }

    public void setOnPositiveClickListener(Action0 action0) {
        this.mPositiveAction = action0;
    }

    public void setOnNegativeClickListener(Action0 action0) {
        this.mNegativeAction = action0;
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder dialog = new AlertDialog.Builder(getActivity())
                .setTitle(mTitle);

        if (!TextUtils.isEmpty(mMessage)) {
            dialog.setMessage(mMessage);
        }

        if (!TextUtils.isEmpty(mPositiveText)) {
            dialog.setPositiveButton(mPositiveText, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                    if (mPositiveAction != null) {
                        mPositiveAction.call();
                    }
                }
            });
        }

        if (!TextUtils.isEmpty(mNegativeText)) {
            dialog.setNegativeButton(mNegativeText, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                    if (mNegativeAction != null) {
                        mNegativeAction.call();
                    }
                }
            });
        }

        return dialog.setCancelable(true).create();
    }

    @Override
    public void show(FragmentManager manager, String tag) {
        super.show(manager, tag);
        new Handler().post(new Runnable() {
            @Override
            public void run() {
                try {
                    Field mAlert = AlertDialog.class.getDeclaredField("mAlert");
                    mAlert.setAccessible(true);
                    Object mAlertController = mAlert.get(getDialog());
                    Field mBtnField = mAlertController.getClass().getDeclaredField("mButtonPositive");
                    mBtnField.setAccessible(true);
                    TextView mBtnPositive = (TextView) mBtnField.get(mAlertController);
                    mBtnPositive.setTextColor(Color.BLUE);
                    mBtnField = mAlertController.getClass().getDeclaredField("mButtonNegative");
                    mBtnField.setAccessible(true);
                    TextView mBtnNegative = (TextView) mBtnField.get(mAlertController);
                    mBtnNegative.setTextColor(Color.BLUE);
                } catch (IllegalAccessException e) {
                    MobclickAgent.reportError(UIUtils.getContext(), e);
                } catch (NoSuchFieldException e) {
                    MobclickAgent.reportError(UIUtils.getContext(), e);
                } catch (NullPointerException e) {
                    MobclickAgent.reportError(UIUtils.getContext(), e);
                }
            }
        });
    }
}
